package com.thrd.taiexhibition.adapter;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;
import com.thrd.basicframework.bean.BaseRecyclerAdapter;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.bean.BeanEnterprationDown;
import com.thrd.taiexhibition.bean.DayEnterprationInfo;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;
import com.thrd.taiexhibition.utils.Constant;

import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/4/1
 */
public class EnterpriseInfoAdapter extends RecyclerView.Adapter {
    private List<DayEnterprationInfo> dayEnterprationInfoList;
    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    public EnterpriseInfoAdapter(Context mContext, List<DayEnterprationInfo> dayEnterprationInfoList){
        this.mContext=mContext;
        this.dayEnterprationInfoList=dayEnterprationInfoList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==0){
            View view=mLayoutInflater.inflate(R.layout.item_enterprise_home,parent,false);
            return new HomeHlder(view);
        }else if(viewType==1){
            View view=mLayoutInflater.inflate(R.layout.item_enterprise_sideslip,parent,false);
            return new SideslipHlder(view);

        }else{
            View view=mLayoutInflater.inflate(R.layout.item_enterprise_image,parent,false);
            return new ImageHlder(view);

        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        DayEnterprationInfo mDayEnterprationInfo= getPositionObject(position);
        if(holder instanceof HomeHlder){
            HomeHlder mHomeHlder= (HomeHlder) holder;
            FrescoUtils.getInstance().setPrefixProgressLoad(mContext,mHomeHlder.mImage,mDayEnterprationInfo.getImageBg());
        }else if(holder instanceof SideslipHlder){
            final SideslipHlder mSideslipHlder= (SideslipHlder) holder;
            mSideslipHlder.mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

            List<BeanEnterprationDown> enterprationDownList=mDayEnterprationInfo.getThComProductContentSideslip();
            if(enterprationDownList!=null&&enterprationDownList.size()>0){
                FrescoUtils.getInstance().setPrefixProgressLoad(mContext,mSideslipHlder.mImagePage,enterprationDownList.get(0).getProductDetailImg());
               // mSideslipHlder.mTitle.setText(TextUtils.isEmpty(enterprationDownList.get(0).getProductDetailTitle())?"":enterprationDownList.get(0).getProductDetailTitle());
            }


            if(mSideslipHlder.mRecyclerView.getAdapter()==null){
                final EnterpriseInfoSideslipAdapter mEnterpriseInfoSideslipAdapter=new EnterpriseInfoSideslipAdapter(mContext,enterprationDownList);
                mEnterpriseInfoSideslipAdapter.setItemClick(new BaseRecyclerAdapter.ItemClick<BeanEnterprationDown>() {
                    @Override
                    public void onItemClick(int p, BeanEnterprationDown mBeanEnterprationDown) {
                        FrescoUtils.getInstance().setPrefixProgressLoad(mContext,mSideslipHlder.mImagePage,mBeanEnterprationDown.getProductDetailImg());
                        mEnterpriseInfoSideslipAdapter.setSelect(p);
                      //  mSideslipHlder.mTitle.setText(TextUtils.isEmpty(mBeanEnterprationDown.getProductDetailTitle())?"":mBeanEnterprationDown.getProductDetailTitle());
                    }
                });
                mSideslipHlder.mRecyclerView.setAdapter(mEnterpriseInfoSideslipAdapter);
            }else{
                EnterpriseInfoSideslipAdapter mEnterpriseInfoSideslipAdapter= (EnterpriseInfoSideslipAdapter) mSideslipHlder.mRecyclerView.getAdapter();
                mEnterpriseInfoSideslipAdapter.refresh(mDayEnterprationInfo.getThComProductContentSideslip());
            }



        }else {
            ImageHlder mImageHlder= (ImageHlder) holder;
          //  FrescoUtils.getInstance().setPrefixProgressLoad(mContext,mImageHlder.mImage,mDayEnterprationInfo.getImageBg());
            setControllerListener(mImageHlder.mImage, Constant.IMAGE+mDayEnterprationInfo.getImageBg(),mContext);
        }
    }

    @Override
    public int getItemCount() {
        return dayEnterprationInfoList==null?0:dayEnterprationInfoList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return dayEnterprationInfoList.get(position).getType();
    }

    private DayEnterprationInfo getPositionObject(int position){
        return dayEnterprationInfoList.get(position);
    }

    public void setData(List<DayEnterprationInfo> dayEnterprationInfoList) {
        this.dayEnterprationInfoList = dayEnterprationInfoList;
        notifyDataSetChanged();
    }

    private class HomeHlder  extends RecyclerView.ViewHolder {


        private final FrescoImage mImage;

        public HomeHlder(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.fi_image);
        }

    }

    private class ImageHlder  extends RecyclerView.ViewHolder {
        private final FrescoImage mImage;

        public ImageHlder(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.fi_image);

        }

    }


    private class SideslipHlder  extends RecyclerView.ViewHolder {

        private final RecyclerView mRecyclerView;
        private final FrescoImage mImagePage;

        public SideslipHlder(@NonNull View itemView) {
            super(itemView);

            mRecyclerView = itemView.findViewById(R.id.rv_recyclerview);
            mImagePage = itemView.findViewById(R.id.fi_image_page);

        }

    }

    /**通过imageWidth 的宽度，自动适应高度
     * @param simpleDraweeView view
     * @param imagePath  Uri
     * @param imageWidth width
     * @param mContext  context
     */
    private   void setControllerListener(final SimpleDraweeView simpleDraweeView, String imagePath, Context mContext) {
        final ViewGroup.LayoutParams layoutParams = simpleDraweeView.getLayoutParams();


        ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
            @Override
            public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                if (imageInfo == null) {
                    return;
                }
                int height = imageInfo.getHeight();
                int width = imageInfo.getWidth();
                layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;//imageWidth;
                layoutParams.height = height;//(int) ((float) (imageWidth * height) / (float) width);
                simpleDraweeView.setLayoutParams(layoutParams);
            }

            @Override
            public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                Log.d("TAG", "Intermediate image received");
            }

            @Override
            public void onFailure(String id, Throwable throwable) {
                throwable.printStackTrace();
            }
        };

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setControllerListener(controllerListener)
                .setUri(Uri.parse(imagePath))
                .build();
        simpleDraweeView.setController(controller);
    }
}
